package com.wld.xm.framework.core.util;

import com.wld.xm.framework.core.common.WebContent;
import org.springframework.util.StringUtils;

import java.util.Base64;
import java.util.LinkedHashMap;

/**
 * @author hxj
 * @version 创建时间：2018年1月3日 上午11:07:39 类说明
 */
public class TokenTimeUtils {

    public static String getTokenKey(LinkedHashMap<String, String> map, String time) {
        String data = "";
        for (String key : map.keySet()) {
            data += key + map.get(key);
        }
        String ret = "";
        if (StringUtils.isEmpty(time)) {
            return ret;
        }
        ret = getTokenKey(data, time);
        return ret;
    }

    public static String getTokenKey(String params, String time) {
        String ret = "";
        if (StringUtils.isEmpty(time)) {
            return ret;
        }
        try {
            String ll = Base64.getEncoder().encodeToString(params.getBytes("utf-8"));
            ll = WebContent.KEY_DATA + time + ll;
            ll = Base64.getEncoder().encodeToString(ll.getBytes());
            ret = MD5Util.getMd5Value(ll);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }

    public static void getUrl(String u, LinkedHashMap<String, String> map, String time, String token) {
        for (String key : map.keySet()) {
            u += key + "=" + map.get(key) + "&";
        }
        u += "time=" + time + "&token=" + token;
        System.out.println(u);
    }


    public static void main(String[] arg) {
        LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
        map.put("id", "5");
        map.put("openid", "e4fa14151c3aac2ced99782d75fc0894");
        String token = getTokenKey(map, "1516348486");
        System.out.println(token);
        getUrl("localhost:9013/xm-valuable-book-consumer/article/accidenceDetail?", map, "1516348486", token);
    }
}
